#ifndef SYSTEMCOMMANDS_H
#define SYSTEMCOMMANDS_H
/** /file
*/
#include <string>
#include <QTime>

namespace SwarmHostSystemCommands{

using std::string;
const string GCC_ARM = "$HOME/tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-gcc ";
const string COMPILE = " -c ";
const string OUTPUT = " -o ";
const string GCC = "gcc ";
const string PROTOMASTER = "$HOME/proto ";
const string COPY_COMMAND = " copy ";
const string REMOTE_COMMAND = " rcmd ";
const string LIB_FOLDER = " /home/pi/lib/";
const string SYS_LIB_FOLDER = "/usr/local/lib/";
const string BIN_FOLDER = " /home/pi/bin/";
const string OBJ_FOLDER = " /home/pi/obj/";
const string MOVE = "mv -f ";
const string CREATE_LIBRARY = " -shared ";
const string CREATE_STATIC = "ar rcs ";
const string TEXT_EDITOR = "gedit";
const string USER_NAME = "pi";
const string PASSWORD  = "raspberry";

inline string systemCallWithOutput(string command){
    string response;
    const size_t s = 25;
    char temp[s] = "";
    // Run command
    FILE * textStream = popen((command +"  2>&1").c_str(), "r");

    // Read all of commands output
    while (!feof(textStream)){
        fgets(temp,s,textStream);
        response += string(temp);
    }
    pclose(textStream);
    return response;
}


inline string SystemCallRemoteCommander(string ip,const string & command){
    string tempUserIP = " "+ USER_NAME + "@" + ip + " " + PASSWORD + " ";
    return systemCallWithOutput(PROTOMASTER + tempUserIP + command);
}

inline string SendRemoteCommand(string ip, const string & command){
    string temp;
    temp = REMOTE_COMMAND + " " + command;
    return SystemCallRemoteCommander(ip,temp);
}
inline string MoveFileFromHostToSwarm(string ip, const string& from,
                                   const string& targetDirectory){
    return SystemCallRemoteCommander(ip, COPY_COMMAND + " " + from + " " +
                                     targetDirectory);
}
inline string CrossCompileFiles(const string& cflags,const string & includes,
                             const string &inputfiles)
{
    string command = GCC_ARM + " " + cflags + " " + includes + " "
            + COMPILE + " " + inputfiles;

    return systemCallWithOutput(command);
}
inline string Remote_GCC(string ip, const string& command){
    return SendRemoteCommand(ip, GCC + " " + command);
}

inline string CreateSharedObjectFile(string ip, const string& outputFilename,
    const string&  objectFiles, const string&  libs){

    string command = CREATE_LIBRARY + " " + OUTPUT + " " + outputFilename +
            " " + libs + " " + objectFiles;

    return Remote_GCC(ip, command);
}

inline string CreateStaticArchiveFile(string ip, const string& outputFilename,
    const string&  objectFiles, const string&  libs){

    string command = CREATE_STATIC + " " + outputFilename +
            " " + libs + " " + objectFiles;

    return SendRemoteCommand(ip, command);
}

inline string UpdateLibraries(string ip, const string& libraries){
    string comm = SendRemoteCommand(ip, ("sudo ln -sf " + libraries +
                                  " --target-directory=" + SYS_LIB_FOLDER));
    SendRemoteCommand(ip,"sudo ldconfig " + SYS_LIB_FOLDER);
    return comm;
}

inline string createExecutable(string ip, const string& outputFilename,
                            const string& objectFiles,
                            const string& libs)
{
    string command = OUTPUT + " " + outputFilename + " " + libs + " "
            + objectFiles;
    return Remote_GCC(ip,command);
}


}

#endif // SYSTEMCOMMANDS_H
